<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
        1.迭代器初体验
     */
    let arr = ['a', 'b', 'c']
    let iter = arr[Symbol.iterator]();
    console.log(iter.next())    // {value:a,done:false}
    console.log(iter.next())    // {value:b,done:false}
    console.log(iter.next())    // {value:c,done:false}
    console.log(iter.next())    // {value:undefined,done:true}

    /*
        2.不同的迭代器实例相互之间没有联系
     */
    let iter1 = arr[Symbol.iterator]()
    let iter2 = arr[Symbol.iterator]()
    console.log(iter1.next())  //{value:a,done:false}
    console.log(iter2.next())  //{value:a,done:false}
    console.log(iter1.next())  //{value:b,done:false}
    console.log(iter2.next())  //{value:b,done:false}
    console.log(iter1 === iter2) // false

    /*
        3.迭代过程中,更改变量,迭代器也会随之改变
     */
    let arr3 = ['a', 'b', 'c']
    let iter3 = arr3[Symbol.iterator]();
    console.log(iter3.next()) //{value:'a',done:false}
    arr3.splice(1, 0, 'add');
    console.log(iter3.next()) // {value:'add',done:false}

    /*
        4.手动实现迭代器
     */
    class Foo {
        [Symbol.iterator]() {
            return {
                next() {
                    return {done: false, value: 'foo'}
                }
            }
        }
    }
    
    let f = new Foo();
    let iterF = f[Symbol.iterator]()
    console.log(iterF.next()) // {done:false:value:'foo'}
    
    
</script>
</body>
</html>